package container;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Container which contains the result of the possibility calculations.
 * This class also contains methods to get a {@link String} representation of
 * the results.
 * 
 * @author Marcus Lindner
 * @author Joana Otetelisanu
 */
public class ProbabilityContainer {

    private Map<Integer, Number> damageProbabilities;
    private Map<Integer, Number> mosProbabilities;
    private Integer mosExpectedValue;
    private Integer damageExpectedValue;
    private static final String NEW_LINE = System.getProperty("line.separator");

    /**
     * Returns a String representation of the possibilities to generate the
     * number of damage foxes.
     * 
     * @return the representation
     */
    public String getDamageBoxProbabilityRepresentation() {
	final StringBuffer stringBuffer = new StringBuffer();
	List<Integer> keys = new ArrayList<Integer>(getDamageProbabilities()
		.keySet());
	Collections.sort(keys);

	Iterator<Integer> iterator = keys.iterator();

	while (iterator.hasNext()) {
	    Integer key = iterator.next();
	    Number prob = getDamageProbabilities().get(key);

	    String damageBoxName = "";
	    String probabilityString = prob.toString()
		+ " ( "+ prob.doubleValue() +" )" + NEW_LINE;

	    if (!iterator.hasNext()) {
		damageBoxName = "P( " + key + "+ Damage boxes ): " + probabilityString;
	    } else {
		damageBoxName = "P( " + key + " Damage boxes ): " + probabilityString;
	    }
	    stringBuffer.append(damageBoxName);
	}

//	final String expectedValue = "Expected number of damage boxes: "
//		+ getDamageExpectedValue() + NEW_LINE;
//	return stringBuffer.append(expectedValue).toString();
	
	return stringBuffer.toString();
    }

    /**
     * Returns a String representation of the possibilities to generate the
     * different MoS.
     * 
     * @return the representation
     */
    public String getMoSProbabilityRepresentation() {
	final StringBuffer stringBuffer = new StringBuffer();

	final List<Integer> mosKeys = new ArrayList<Integer>(
		this.mosProbabilities.keySet());
	Collections.sort(mosKeys);

	Iterator<Integer> iterator = mosKeys.iterator();

	while (iterator.hasNext()) {
	    Integer key = iterator.next();
	    
	    Number prob = getMosProbabilities().get(key);

	    String mosLine = "P( MoS " + key + " ): " + prob + " ( " + prob.doubleValue() +" )"+ NEW_LINE;

	    stringBuffer.append(mosLine);
	}

	//Integer expectedMoS = getMosExpectedValue();

//	final String expectedValue = "Expected MoS: " + expectedMoS + NEW_LINE;
//	return stringBuffer.append(expectedValue).toString();
	
	return stringBuffer.toString();
    }

    /**
     * Sets the expected value for the MoS
     * 
     * @param expectedValue
     */
    public void setMosExpectedValue(Integer expectedValue) {
	this.mosExpectedValue = expectedValue;
    }

    /**
     * Sets the expected value for the damage boxes.
     * 
     * @param expectedValue
     */
    public void setDamageExpectedValue(Integer expectedValue) {
	this.damageExpectedValue = expectedValue;
    }

    /**
     * Returns the expected value for the damage boxes.
     * 
     * @return the expected value
     */
    public Integer getDamageExpectedValue() {
	return this.damageExpectedValue;
    }

    /**
     * @return the mosExpectedValue
     */
    public Integer getMosExpectedValue() {
	return this.mosExpectedValue;
    }

    /**
     * @return the damageProbabilities
     */
    public Map<Integer, Number> getDamageProbabilities() {
	return this.damageProbabilities;
    }

    /**
     * @return the mosProbabilities
     */
    public Map<Integer, Number> getMosProbabilities() {
	return this.mosProbabilities;
    }

    /**
     * @param newMosProbabilities the mosProbabilities to set
     */
    public void setMosProbabilities(Map<Integer, Number> newMosProbabilities) {
	this.mosProbabilities = newMosProbabilities;
    }

    /**
     * @param damageProbability
     */
    public void setDamageProbabilities(Map<Integer, Number> damageProbability) {
	this.damageProbabilities = damageProbability;
    }

}
